<template>
  <div class="choose_members">
    <el-dialog
        custom-class="choose_dialog"
        :visible.sync="chooseUserData.chooseUserState"
        width="840px"
        :show-close="false"
        :close-on-click-modal="false">

        <el-dialog
          :visible.sync="createGroupDialog"
          width="290px"
          :show-close="false"
          :close-on-click-modal="false"
          append-to-body>

          <div class="dialog_title">设置群昵称</div>
          <div class="input_box single_line_box">
            <input class="input_text single_line_input" maxlength="30" v-model="groupNameText" placeholder="请填写群昵称" />
            <div class="input_num">{{groupNameText.length}}/30</div>
          </div>
          <div class="btns_box">
            <div class="primarybtn default" @click="cancelCreateGroup">取消</div>
            <div class="primarybtn" @click="confirmCreateGroup">创建群</div>
          </div>
        </el-dialog>

        <div class="dialog_top">
            <div class="dialog_title">{{diaLogName}}</div>
            <img class="cancel_dialog_btn" src="../../assets/icons/icon_close_up.png" @click="cancelDialog" />
        </div>

        <div class="choose_box">
          <div class="all_members">
            <!-- <div class="member_search">
              <div class="search_content">
                  <img class="search_icon" src="../../assets/choose.png"/>
                  <input class="search_input" type="text" placeholder="输入昵称" maxlength="30" v-model="memberText"/>
              </div>
            </div> -->

            <ul class="friends_box">
              <!-- <div class="pingyin_list" v-for="(item,index) in list" :key="index">
                <div class="pingyin_first">{{item.title}}</div>
                <li class="friends_line" v-for="(data,dataIndex) in item.children" :key="dataIndex" @click="chooseFriend(data)">
                  <img class="friends_avatar" :src="data.img" />
                  <span class="friends_name">{{data.name}}</span>
                  <img class="choose_icon" v-show="!data.choose" src="../../assets/choose.png" />
                  <img class="choose_icon" v-show="data.choose" src="../../assets/choosed.png" />
                </li>
              </div> -->
              <li class="friends_line" v-for="(item,index) in list" :key="index" @click="chooseFriend(item)">
                <img class="friends_avatar" :src="item.friendHeadImg||item.userHeadImg" />
                <span class="friends_name">{{item.friendRemarks||item.nickName}}</span>
                <img class="choose_icon" v-show="!item.choose" src="../../assets/choose.png" />
                <img class="choose_icon" v-show="item.choose" src="../../assets/choosed.png" />
              </li>
            </ul>
          </div>

          <div class="all_choosed">
            <div class="choosed_top">
              <div class="choosed_tips">请勾选要要加的联系人<span v-show="choosedList.length!=0">({{choosedList.length}})</span></div>
              <div :class="['primarybtn',{'disabled':choosedList.length==0}]" @click="dealBtn(1)">{{diaLogName}}</div>
            </div>
            
            <ul class="choosed_list">
              <li class="choosed_member" v-for="(item,index) in choosedList" :key="index">
                <img class="choosed_avatar" :src="item.friendHeadImg||item.userHeadImg" />
                <div class="choosed_name">{{item.friendRemarks||item.nickName}}</div>
                <img class="cancel_choosed" src="../../assets/icons/icon_delete_up.png" @click="cancelChoosed(item,index)" />
              </li>
            </ul>
          </div>
        </div>
    </el-dialog>
  </div>
</template>

<script>
import {Dialog,Message} from 'element-ui'
import {createGroup,groupList,deleteGroupMember,groupMemberList,groupFriendList,addGroupMember} from '../../api/api'
export default {
  name:'choose_members',
  data(){
      return {
        searchText:'',
        memberText:'',
        choosedList:[],
        diaLogName:'',
        list:[],
        createGroupDialog:false,
        groupNameText:'',
        // list:
        // [
        //   {
        //     title:'A',
        //     children:[
        //       {
        //         choose:false,
        //         img:'https://thirdwx.qlogo.cn/mmopen/vi_32/OQD4fMO3kYuJxibwZzlXy7cKs0Ech85xj7nAeHgFIA2sXJRkHFfaXhkia0WbyCmtzpmhPuJMbAwWPRu2VqVvnnBQ/132',
        //         name:'测试',
        //         id:1
        //       },
        //       {
        //         choose:false,
        //         img:'https://thirdwx.qlogo.cn/mmopen/vi_32/OQD4fMO3kYuJxibwZzlXy7cKs0Ech85xj7nAeHgFIA2sXJRkHFfaXhkia0WbyCmtzpmhPuJMbAwWPRu2VqVvnnBQ/132',
        //         name:'猫',
        //         id:2
        //       }
        //     ]
        //   },
        //   {
        //     title:'C',
        //     children:[
        //       {
        //         choose:false,
        //         img:'https://thirdwx.qlogo.cn/mmopen/vi_32/OQD4fMO3kYuJxibwZzlXy7cKs0Ech85xj7nAeHgFIA2sXJRkHFfaXhkia0WbyCmtzpmhPuJMbAwWPRu2VqVvnnBQ/132',
        //         name:'测试',
        //         id:3
        //       },
        //       {
        //         choose:false,
        //         img:'https://thirdwx.qlogo.cn/mmopen/vi_32/OQD4fMO3kYuJxibwZzlXy7cKs0Ech85xj7nAeHgFIA2sXJRkHFfaXhkia0WbyCmtzpmhPuJMbAwWPRu2VqVvnnBQ/132',
        //         name:'猫',
        //         id:4
        //       }
        //     ]
        //   }
        // ]
      }
  },
  props:{
    chooseUserData:Object
  },
  components: {
      'el-dialog':Dialog
  },
  watch:{
    'chooseUserData.type'(){
      console.log('chooseUserData',this.chooseUserData)
      this.list = []
      this.choosedList = []
      if(this.chooseUserData.type=="createGroup"){
        this.diaLogName = '创建群聊'
        this.list = JSON.parse(JSON.stringify(this.$store.state.friendList))
      }else if(this.chooseUserData.type=="deleteMember"){
        this.diaLogName = '删除成员'
        this.list = JSON.parse(JSON.stringify(this.$store.state.groupMembersList))
      }else if(this.chooseUserData.type=="addMember"){
        this.diaLogName = '添加成员'
        let params = {
          userId:this.$store.state.user.id,
          roomId:this.$store.state.choosedGroupId,
          // friendRemarks:this.$store.state.user.userName
        }
        groupFriendList(params)
        .then(res => {
          this.list = res.data.data.list
        })
      }
    }
  },
  mounted(){
    
  },
  methods:{
    cancelDialog(){
      this.searchText = ''
      this.$emit('changeChooseState',{
        type:'',
        chooseUserState:false
      })
    },
    //关闭创建群
    cancelCreateGroup(){
      this.createGroupDialog = false
    },
    confirmCreateGroup(){
      this.dealBtn()
      this.cancelCreateGroup()
    },
    //选择好友
    chooseFriend(item){
      this.list.forEach((element)=>{
        if(this.chooseUserData.type=="createGroup"){
          var elementId = element.friendId
          var itemId = item.friendId
        }else if(this.chooseUserData.type=="deleteMember"){
          var elementId = element.userId
          var itemId = item.userId
        }else if(this.chooseUserData.type=="addMember"){
          var elementId = element.friendId
          var itemId = item.friendId
        }
        if(elementId==itemId){
          if(item.choose){
            this.$set(item,'choose',false)
            this.choosedList.forEach((content,i)=>{
              if(this.chooseUserData.type=="createGroup"){
                var contentId = content.friendId
              }else if(this.chooseUserData.type=="deleteMember"){
                var contentId = content.userId
              }else if(this.chooseUserData.type=="addMember"){
                var contentId = content.friendId
              }
              if(itemId==contentId){
                this.choosedList.splice(i,1)
              }
            })
          }else{
            this.$set(item,'choose',true)
            this.choosedList.push(item)
          }
        }
      })
    },
    //取消选择
    cancelChoosed(item,index){
      this.list.forEach((element)=>{
        if(this.chooseUserData.type=="createGroup"){
          var elementId = element.friendId
          var itemId = item.friendId
        }else if(this.chooseUserData.type=="deleteMember"){
          var elementId = element.userId
          var itemId = item.userId
        }else if(this.chooseUserData.type=="addMember"){
          var elementId = element.friendId
          var itemId = item.friendId
        }
        if(elementId==itemId){
          this.$set(element,'choose',false)
        }
      })
      this.choosedList.splice(index,1)
    },
    dealBtn(inToNum){
      if(this.choosedList.length==0){
        return false
      }
      var arr = []
      var nameArr = []
      this.choosedList.forEach((element)=>{
        if(this.chooseUserData.type=="createGroup"){
          var elementId = element.friendId
          nameArr.push(element.friendRemarks)
        }else if(this.chooseUserData.type=="deleteMember"){
          var elementId = element.userId
        }else if(this.chooseUserData.type=="addMember"){
          var elementId = element.friendId
          nameArr.push(element.friendRemarks)
        }
        arr.push(elementId)
      })
      var ids = arr.join(',')
      if(nameArr.length!=0){
        var names = nameArr.join(',')
      }

      //创建群聊
      if(this.chooseUserData.type=="createGroup"){
        if(inToNum===1){
          this.createGroupDialog = true
          return false
        }
        
        let params = {
          masterId:this.$store.state.user.id,
          roomName:this.groupNameText,
          roomImg:'http://sixeco-redt-b.oss-cn-shanghai.aliyuncs.com/EF7EF28D78DE4A65AA62FD81B461AB89.',
          userIds:ids
        }
        createGroup(params)
        .then(res => {
          //关联群聊的人
          var sendData = {
            cmd:7,
            message:{
              content:names,
              contentImg:"",
              contentVideo:"",
              messageType:0,
              mine:false,
              sendTime:new Date().getTime(),
              sendUserId:this.$store.state.user.id,
              sendUserImg:this.$store.state.user.headImg,
              sendUserName:this.$store.state.user.userName,
              toId:res.data.data.id,
              toUserImg:res.data.data.roomImg,
              toUserName:res.data.data.roomName
            },
            type:42,
            userIds:ids
          }
          console.log('创建群关联的实体',sendData)
          ws.send(JSON.stringify(sendData))

          //刷新群列表
          let params1 = {
              pageNo:1,
              pageSize:500,
              masterId:this.$store.state.user.id
          }
          groupList(params1)
          .then(res => {
              var list = res.data.data.list
              this.$store.commit('getGroupList',list)
              this.$store.commit('setNav','group')
          })
          Message({
            message: '创建群聊成功',
            type: 'success'
          })
        })
      //批量移除群成员
      }else if(this.chooseUserData.type=="deleteMember"){
        let params = {
          roomId:this.$store.state.choosedGroupId,
          userIds:ids
        }
        deleteGroupMember(params)
        .then(res => {
          let params1 = {
              pageNo:1,
              pageSize:500,
              roomName:this.$store.state.groupName,
              roomId:this.$store.state.choosedGroupId
          }
          //加载群成员列表
          groupMemberList(params1)
          .then(res => {
            this.$store.commit('chooseGroup',this.$store.state.choosedGroupId)
            this.$store.commit('getGroupMembersList',res.data.data.list)
          })
          Message({
            message: '移出群聊成功',
            type: 'success'
          })
        })
      //批量添加群成员
      }else if(this.chooseUserData.type=="addMember"){
        let params = {
          roomId:this.$store.state.choosedGroupId,
          userIds:ids
        }
        addGroupMember(params)
        .then(res => {
          //关联群聊的人
          var sendData = {
            cmd:7,
            message:{
              content:names,
              contentImg:"",
              contentVideo:"",
              messageType:0,
              mine:false,
              sendTime:new Date().getTime(),
              sendUserId:this.$store.state.user.id,
              sendUserImg:this.$store.state.user.headImg,
              sendUserName:this.$store.state.user.userName,
              toId:this.$store.state.choosedGroupId,
              toUserImg:this.$store.state.groupImg,
              toUserName:this.$store.state.groupName
            },
            type:42,
            userIds:ids
          }
          console.log('添加群成员关联的实体',sendData)
          ws.send(JSON.stringify(sendData))

          let params1 = {
              pageNo:1,
              pageSize:500,
              roomName:this.$store.state.groupName,
              roomId:this.$store.state.choosedGroupId
          }
          //加载群成员列表
          groupMemberList(params1)
          .then(res => {
            this.$store.commit('chooseGroup',this.$store.state.choosedGroupId)
            this.$store.commit('getGroupMembersList',res.data.data.list)
          })
          Message({
            message: '添加成员成功',
            type: 'success'
          })
        })
      }
      this.cancelDialog()
    },
    // //选择好友
    // chooseFriend(data){
    //   this.list.forEach((element)=>{
    //     element.children.forEach((item,index)=>{
    //       if(item.id==data.id){
    //         if(data.choose){
    //           this.$set(item,'choose',false)
    //           this.choosedList.forEach((content,i)=>{
    //             if(data.id==content.id){
    //               this.choosedList.splice(i,1)
    //             }
    //           })
    //         }else{
    //           this.$set(item,'choose',true)
    //           this.choosedList.push(data)
    //         }
    //       }
    //     })
    //   })
    // },
    // //取消选择
    // cancelChoosed(data,index){
    //   this.list.forEach((element)=>{
    //     element.children.forEach((item,index)=>{
    //       if(item.id==data.id){
    //         this.$set(item,'choose',false)
    //       }
    //     })
    //   })
    //   this.choosedList.splice(index,1)
    // }
  }
}
</script>

<style scoped>
.dialog_top{
    display: flex;
    align-items: center;
    justify-content: space-between;
    line-height: 42px;
    padding: 0 25px 0 30px;
}
.cancel_dialog_btn{
    width:24px;
    height:24px;
    cursor: pointer;
}
.member_search{
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.member_search .search_icon{
    padding:0px 8px;
}
.search_input{
    min-width:280px;
    color: #428dfc;
    line-height: 30px;
    border:none;
}
.search_icon{
    width:20px;
}

.choose_box{
  display: flex;
  padding:0px 30px 30px;
}
.all_members{
  width: 360px;
  margin-right: 30px;
}
.friends_box{
  height:360px;
  overflow: auto;
}
.pingyin_first{
  font-size: 12px;
  line-height: 20px;
  background: #fcfcfc;
}
.friends_avatar{
  width:34px;
  height:34px;
}
.friends_line{
  padding: 10px 0;
  height: 54px;
  cursor: pointer;
  display: flex;
  align-items: center;
  box-sizing: border-box;
}
.friends_name{
  padding:0 10px;
  flex:1;
}
.choose_icon{
  width:18px;
}
.all_choosed{
  flex:1;
}
.choosed_top{
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.choosed_tips{
  flex:1;
  height:40px;
  line-height: 40px;
}

.choosed_list{
  height:360px;
  overflow: auto;
}
.choosed_member{
  padding: 10px 0;
  height: 54px;
  display: flex;
  align-items: center;
  box-sizing: border-box;
}
.choosed_avatar{
  width:34px;
  height:34px;
}
.choosed_name{
  padding:0 10px;
  flex:1;
}
.cancel_choosed{
  width:18px;
  height:18px;
  cursor: pointer;
}


.dialog_title{
  display: flex;
  align-items: center;
  line-height: 42px;
  padding: 0 25px 0 30px;
}
.input_box{
  border: 1px solid #f1f1f1;
  padding: 6px 7px 3px;
  border-radius: 2px;
  margin: 0 30px 30px;
  height: 180px;
}
.input_text{
  outline: none;
  border: none;
  resize: none;
  font-size: 12px;
  width: 100%;
  padding: 0;
  height: 150px;
}
.input_num{
  color: #999;
  float: right;
  padding-right: 7px;
  font-size: 12px;
}
.btns_box{
  display: flex;
  align-items: center;
  justify-content: center;
  padding-bottom: 30px;
}
.primarybtn.default{
  margin-left: 0px;
}
.primarybtn{
  margin-left:46px;
}
.input_box.single_line_box{
  display: flex;
  align-items: center;
  height: 30px;
}
.input_text.single_line_input{
  flex:1;
  height: 100%;
}
</style>